#       FeedConfigTools_test.py
#       
#       Copyright 2008 Joshua Scotton <josh@joshuascotton.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2008, Joshua Scotton"
__author__ = "Joshua Scotton <josh@joshuascotton.com>"

import unittest
import sys
import os
import ConfigParser
import logging

from ConfigParser import ParsingError

try:
 	import pygtk
  	pygtk.require("2.4")
except:
  	pass
try:
	import gtk
  	import gtk.glade
except:
	sys.exit(1)

sys.path += [os.path.abspath('..')]

from utils.feed_utils import OPMLParser, FeedConfigTools

class FeedConfigToolsTest(unittest.TestCase):

    def setUp(self):
        """Sets up everything for the test"""
        
        #set up logger
        self.logger = logging.getLogger('Entertainer')
        fileHandle = logging.FileHandler('../../../tests.log')
        fileHandle.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(funcName)s %(message)s'))
        self.logger.addHandler(fileHandle)
        self.logger.setLevel(logging.DEBUG)
        
        #initialise config file
        self.configPath = './data/FeedConfigTools/content.conf'
        self.config = ConfigParser.ConfigParser()
        try:
            self.config.readfp(open(os.path.expanduser(self.configPath)))
        except:
            print "Error with content.conf in FeedConfigToolsTest setUp()"
            sys.exit(1)
            
        #setup feeds and model
        self.feeds = self.config.get("RSS", "feeds").split(';')
        self.model = gtk.ListStore(str)
        for i in range(len(self.feeds)):
            self.model.insert(i, [self.feeds[i]])
            
        self.files = ["./data/FeedConfigTools/test.opml"]
        self.feedList = ["http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml","http://www.hackaday.com/rss.xml","http://edevelop.org/planet/rss20.xml","http://feeds.feedburner.com/BlenderartMagazine"]
        
    def tearDown(self):
        """restores config to how it should be to start off"""
        self.config.set("RSS","feeds", "http://www.joshuascotton.com/main/feed")
        self.logger.debug("About to refresh test config file")
        try:
            self.config.write(file(self.configPath, 'w'))
            self.logger.debug("Refreshed test config file")
        except:
            self.logger.debug("Can't refresh test config file")
            print "configuration file error, please take a look at: " + self.configPath
    
    def testAddFileFeedsToWidget001(self):
        """testAddFileFeedsToWidget001: tests if self.feeds is updated correctly"""
        FeedConfigTools().add_file_feeds_to_widget(self.files, self.config, self.model, self.feeds, self.configPath, False)
        self.logger.debug("OUTPUT: self.feeds = " + str(self.feeds))
        self.assertEqual(self.feeds, ["http://www.joshuascotton.com/main/feed","http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml","http://www.hackaday.com/rss.xml","http://edevelop.org/planet/rss20.xml","http://feeds.feedburner.com/BlenderartMagazine"])
    
    def testAddFileFeedsToWidget002(self):
        """testAddFileFeedsToWidget002: tests if self.config is updated correctly"""
        FeedConfigTools().add_file_feeds_to_widget(self.files, self.config, self.model, self.feeds, self.configPath, False)
        self.logger.debug("OUTPUT: self.config.get(\"RSS\", \"feeds\").split(';') = " + str(self.config.get("RSS", "feeds").split(';') ))
        self.assertEqual(self.config.get("RSS", "feeds").split(';'),["http://www.joshuascotton.com/main/feed","http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml","http://www.hackaday.com/rss.xml","http://edevelop.org/planet/rss20.xml","http://feeds.feedburner.com/BlenderartMagazine"])
    
    def testAddFeedsToWidget001(self):
        """testAddFeedsToWidget001: tests if self.feeds is updated correctly"""
        FeedConfigTools().add_feeds_to_widget(self.feedList, self.config, self.model, self.feeds, self.configPath)
        self.logger.debug("OUTPUT: self.feeds = " + str(self.feeds))
        self.assertEqual(self.feeds, ["http://www.joshuascotton.com/main/feed","http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml","http://www.hackaday.com/rss.xml","http://edevelop.org/planet/rss20.xml","http://feeds.feedburner.com/BlenderartMagazine"])
        
    def testAddFeedsToWidget002(self):
        """testAddFeedsToWidget002: tests if self.config is updated correctly"""
        FeedConfigTools().add_feeds_to_widget(self.feedList, self.config, self.model, self.feeds, self.configPath)
        self.logger.debug("OUTPUT: self.config.get(\"RSS\", \"feeds\").split(';') = " + str(self.config.get("RSS", "feeds").split(';')))
        self.assertEqual(self.config.get("RSS", "feeds").split(';'), ["http://www.joshuascotton.com/main/feed","http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml","http://www.hackaday.com/rss.xml","http://edevelop.org/planet/rss20.xml","http://feeds.feedburner.com/BlenderartMagazine"])
        
